//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================

#include <mantle.h>

// Interface functions
//
PInterface CEvent::Probe(
    /* [in] */ REIID riid)
{
    if (riid == EIID_IInterface) {
        return (PInterface)(IEvent *)this;
    }
    else if (riid == EIID_IEvent) {
        return (IEvent *)this;
    }
    else if (riid == EIID_CLASS_INFO) {
        return (PInterface)&ECLSID_CEvent;
    }
    else if (riid == EIID_INTERFACE_INFO) {
        return (PInterface)&EIID_IEvent;
    }

    return NULL;
}

UInt32 CEvent::AddRef(void)
{
    return (UInt32)m_cRef.Increment();
}

UInt32 CEvent::Release(void)
{
    Int32 nRef = m_cRef.Decrement();

    if (nRef == 0) {
        delete this;
    }
    return (UInt32)nRef;
}

ECode CEvent::Aggregate(
    /* [in] */ AggregateType type,
    /* [in] */ PInterface pObj)
{
    return E_NOT_IMPLEMENTED;
}

ECode CEvent::GetDomain(
    /* [out] */ PInterface *ppObj)
{
    return E_NOT_IMPLEMENTED;
}

ECode CEvent::Wait(
    /* [out] */ WaitResult *pResult,
    /* [out] */ EventState *pState)
{
    if (NULL == pResult) return E_INVALID_ARGUMENT;

    m_kevent.Wait(pResult, pState);
    return NOERROR;
}

ECode CEvent::TryWait(
    /* [in] */ Millisecond32 msTimeout,
    /* [out] */ WaitResult *pResult,
    /* [out] */ EventState *pState)
{
    if (NULL == pResult) return E_INVALID_ARGUMENT;

    m_kevent.TryWait(
                DzMillisecondsToTicks(msTimeout), pResult, pState);
    return NOERROR;
}

ECode CEvent::Notify(EventState state)
{
    if (UNSIGNALED == state) return E_INVALID_ARGUMENT;

    m_kevent.Notify(state);
    return NOERROR;
}

ECode CEvent::Clear()
{
    m_kevent.Clear();
    return NOERROR;
}

void CEvent::NotifyByIsr(EventState state)
{
    m_kevent.NotifyByIsr(state);
}
